package com.szliang.threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 描述：加锁解决线程安全问题
 *
 * 缺点：线程排队，高并发下性能低
 */
public class ThreadLocalNormalUsage04 {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 10000; i++) {
            int finalI = i;
            threadPool.submit(() -> {
                String date = new ThreadLocalNormalUsage01().date(finalI);
                System.out.println(date);
            });
        }
        threadPool.shutdown();
    }

    public String date(int seconds) {
        //参数单位是毫秒 从1970.1.1 08:00:00 GMT计时（东8区）
        Date date = new Date(seconds*1000);
        String format = null;
        synchronized (ThreadLocalNormalUsage04.class){
            format = dateFormat.format(date);
        }
        return format;
    }
}
